﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xf.Entitys;
using xf.Service;

namespace xf
{
    public partial class FCustomerList : Form
    {
        private static FCustomerList fSaveCustomer = null;

        /// <summary>
        /// 保存/修改客户信息界面，只能同时显示一个
        /// </summary>
        /// <returns></returns>
        public static FCustomerList CreateInstance()
        {
            if ((fSaveCustomer == null) || fSaveCustomer.IsDisposed)
            {
                fSaveCustomer = new FCustomerList();
            }
            return fSaveCustomer;
        }

        public FCustomerList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取查询客户列表
        /// </summary>
        /// <param name="searchCustomerParam"></param>
        public void GetCustomerList(Dictionary<string, string> searchCustomerParam)
        {
            customerDataGridView.DataSource = CustomerService.GetInstance().GetCustomer(searchCustomerParam);
        }

        /// <summary>
        /// 更新查询客户列表
        /// </summary>
        private void ReloadCustomerList()
        {
            Dictionary<string, string> searchCustomerParam = new Dictionary<string, string> {
                { "Cus_id", customerIdTextBox.Text},
                { "Cus_name", customerNameTextBox.Text},
            };
            GetCustomerList(searchCustomerParam);
        }

        public static void StaticReloadCustomerList()
        {
            Dictionary<string, string> searchCustomerParam = new Dictionary<string, string> {
                { "Cus_id", fSaveCustomer.customerIdTextBox.Text},
                { "Cus_name", fSaveCustomer.customerNameTextBox.Text},
            };
            fSaveCustomer.GetCustomerList(searchCustomerParam);
        }

        /// <summary>
        /// 加载的时候触发查询客户列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadCustomerList(object sender, EventArgs e)
        {
            ReloadCustomerList();
        }

        /// <summary>
        /// 搜索客户列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void triggerSearchCustomer(object sender, EventArgs e)
        {
            ReloadCustomerList();
        }


        /// <summary>
        /// 显示添加客户界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addCustomerBtn_Click(object sender, EventArgs e)
        {
            FSaveCustomer addCustomer = new FSaveCustomer();
            addCustomer.Show();
        }

        private void customerDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            DataGridViewCell dataGridViewCell = customerDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
            CUS_NAME cusName = customerDataGridView.Rows[e.RowIndex].DataBoundItem as CUS_NAME;
            if (dataGridViewCell is DataGridViewLinkCell)
            {
                string operation = dataGridViewCell.FormattedValue.ToString();
                switch (operation)
                {
                    case "修改":
                        // 触发新增保存客户页面
                        FSaveCustomer addCustomer = new FSaveCustomer(cusName.Cus_id);
                        addCustomer.Show();
                        break;
                    case "删除":
                        DialogResult dialogResult = MessageBox.Show("您确定要退出删除该客户吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dialogResult == DialogResult.Yes)
                        {
                            // 删除操作
                            Dictionary<string, string> detail = new Dictionary<string, string> { };
                            if (CustomerService.GetInstance().RemoveCustomer(cusName.Cus_id.ToString(), ref detail)) 
                            {
                                // 后期优化，这里需要重新手动刷新一遍
                                ReloadCustomerList();
                            }
                            else
                            {
                                detail.TryGetValue("msg", out string msg);
                                if (string.IsNullOrEmpty(msg)) {
                                    msg = "删除客户失败，客户ID：" + cusName.Cus_id.ToString();
                                }
                                MessageBox.Show(msg);
                            }
                        }
                        break;
                    default:
                        MessageBox.Show("获取客户列表操作异常");
                        break;
                }
            }
        }
    }
}
